package com.alm.token.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.GlideApp;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.base.BaseAdapter;
import com.alm.token.common.RefreshLoadMoreManager;
import com.alm.token.ui.data.bean.MiningWalletTypeIndexData;
import com.alm.token.ui.data.bean.RegisterData;
import com.alm.token.ui.data.bean.StakingHomeData;
import com.alm.token.ui.data.bean.StakingMiningMoneyDrawIndexData;
import com.alm.token.ui.data.bean.StakingOrderAddData;
import com.alm.token.ui.data.bean.StakingOrderAppendData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.StakingView;
import com.alm.token.utils.ActionBarHelper;
import com.alm.token.utils.CommonUtil;
import com.alm.token.utils.EmptyUtils;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class StakingSplashActivity extends BaseActivity implements StakingView<StakingHomeData> {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.tv_clickreload)
    TextView tvClickreload;
    @BindView(R.id.nodatashow_body)
    RelativeLayout nodatashowBody;
    private int ActivityForResultCode = 1;
    private RefreshLoadMoreManager<StakingHomeData.DataBean.GoodsBean> manager;
    private List<StakingHomeData.DataBean.GoodsBean> dataBeans = new ArrayList<>();


    public StakingSplashActivity() {
    }

    @Override
    protected MyPreenter initPreenter() {
        return new MyPreenter(this);
    }


    @Override
    protected int getContentViewId() {
        return R.layout.activity_staking_splash;
    }

    @Override
    protected void initVariables() {

    }


    @Override
    protected void initViews() {
        new ActionBarHelper(this).setTitle(getString(R.string.fortune_hint_poskuang)).setRightText(getString(R.string.title_hint_item13), this);
        initManager();
    }

    private void initManager() {
        OrderAdapter adapter = new OrderAdapter(this);
        manager = new RefreshLoadMoreManager.Builder(this)
                .setAdapter(adapter)
                .setRecyclerView(recyclerView)
                .setOnListItemClickListener(new RefreshLoadMoreManager.OnListItemClickListener() {
                    @Override
                    public void onListItemClick(BaseAdapter.BaseItem item, int position) {
//                        onViewClicked(rlyItem2);
                    }
                })
                .setSwipeRefreshLayout(swipeRefreshLayout)
                .setOnRefreshListener(new RefreshLoadMoreManager.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
                        swipeRefreshLayout.setRefreshing(false);
                        loadData();
                    }
                })
                .build();
        recyclerView.setNestedScrollingEnabled(false);
    }

    @Override
    protected void loadData() {
        preenter.getStakingHomeData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rightItem:
                startActivity(StakingOrderHistoryActivity.class);
                break;
        }
    }

    @OnClick(R.id.tv_clickreload)
    public void onViewClicked() {
        loadData();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ActivityForResultCode) {
            if (resultCode == QuantitativeOrderOpenAddActivity.AddAppendSucc
                    || resultCode == QuantitativeOrderDetailActivity.AppendSucc) {
//                showToast("添加成功，刷新列表");
                loadData();
                setResult(QuantitativeOrderOpenAddActivity.AddAppendSucc);
//                finish();
            }

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @Override
    public void getData(StakingHomeData stakingHomeData) {

        if (stakingHomeData.getStatus() == 0) {
            if (EmptyUtils.isNotEmpty(stakingHomeData.getData())) {
                List<StakingHomeData.DataBean.GoodsBean> goods = stakingHomeData.getData().getGoods();
                dataBeans.clear();
                if (EmptyUtils.isNotEmpty(goods)) {
                    for (StakingHomeData.DataBean.GoodsBean goodsBean : goods) {
                        /**
                         * id : 1
                         * moneyType : 1
                         * status : 1
                         * icon : https://w.iotoken.net/static/images/35656046013.png
                         * name : ETH
                         * waitNum : 0
                         * waitHour : 24
                         */
                        goodsBean.setLayoutId(OrderAdapter.LAYOUT_ID);
                        dataBeans.add(goodsBean);
                    }
                    Collections.sort(dataBeans);
                }
                for (int i = 0; i < dataBeans.size(); i++) {
                    if (dataBeans.get(i).getStatus() == 1) {
                        dataBeans.get(i).setViewType(1);
                        break;
                    }
                }
                for (int i = 0; i < dataBeans.size(); i++) {
                    if (dataBeans.get(i).getStatus() == 0) {
                        dataBeans.get(i).setViewType(1);
                        break;
                    }
                }

                manager.setData(dataBeans);
            }
            if (dataBeans.size() == 0) {
                swipeRefreshLayout.setVisibility(View.GONE);
                nodatashowBody.setVisibility(View.VISIBLE);
            } else {
                swipeRefreshLayout.setVisibility(View.VISIBLE);
                nodatashowBody.setVisibility(View.GONE);
            }
        } else {
            showToast(stakingHomeData.getMsg());
        }
    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast("需要重新登录!");
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(getActivity(),PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType",PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            getActivity().finish();
            showToSetPaypswDialog();
        } else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {
        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {
        showWaitDialog();
    }

    @Override
    public void hideProgress() {
        hideWaitDialog();
    }

    @Override
    public void getStakingAddOrderData(StakingOrderAddData stakingOrderAddData) {

    }

    @Override
    public void getStakingCancelQueuingData(RegisterData registerData) {

    }

    @Override
    public void getStakingPayOrderData(RegisterData registerData) {

    }

    @Override
    public void getStakingOrderAppendData(StakingOrderAppendData appendData) {

    }

    @Override
    public void getStakingMiningMoneyDrawIndexData(StakingMiningMoneyDrawIndexData stakingMiningMoneyDrawIndexData) {

    }

    @Override
    public void getMiningWalletTypeIndexData(MiningWalletTypeIndexData miningWalletTypeIndexData) {

    }

    @Override
    public void getDrawIndexData(Object o) {

    }


    public class OrderAdapter extends BaseAdapter<StakingHomeData.DataBean.GoodsBean> {

        public static final int LAYOUT_ID = R.layout.item_staking_splashorder;

        public OrderAdapter(@NonNull Context context) {
            super(context);
        }

        @Override
        protected void convert(BaseViewHolder helper, StakingHomeData.DataBean.GoodsBean item, int itemType) {
            /**
             * id : 1
             * moneyType : 1
             * status : 1
             * icon : https://w.iotoken.net/static/images/35656046013.png
             * name : ETH
             * waitNum : 0
             * waitHour : 24
             */

            helper.setGone(R.id.tv_openshow, item.getStatus() == 1 && item.getViewType() == 1 ? true : false);
            helper.setGone(R.id.tv_notopenshow, item.getStatus() == 0 && item.getViewType() == 1 ? true : false);
            ImageView imageView = (ImageView) helper.getView(R.id.img_order_tubiao);
            GlideApp.with(context).load(item.getIcon())
                    .error(R.drawable.icon_arctic).into(imageView);
            helper.setText(R.id.tv_tubiaoName, item.getName());
            helper.setText(R.id.tv_waittime, getString(R.string.staking_hint_staking_waittime, String.valueOf(item.getWaitHour())));
            helper.setText(R.id.tv_waitnum, getString(R.string.staking_hint_staking_waittnum, String.valueOf(item.getWaitNum())));
            if (item.getStatus() == 1) {

                //可以挖矿
//                helper.setText(R.id.tv_order_gonow, getString(R.string.staking_hint_staking_nowgo));
                helper.setText(R.id.tv_order_gonow, EmptyUtils.isNotEmpty(item.getOrderStatusName())?
                        item.getOrderStatusName():getString(R.string.staking_hint_staking_nowgo));
                helper.setGone(R.id.tv_waittime, true);
                helper.setGone(R.id.tv_waitnum, true);
                helper.setOnClickListener(R.id.tv_order_gonow, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        showToast(item.getNe());
                        Intent intent = new Intent(StakingSplashActivity.this, StakingOrderIndexActivity.class);
                        intent.putExtra("moneyType", item.getMoneyType());
                        intent.putExtra("orderSn", "");
                        startActivityForResult(intent, ActivityForResultCode);
                    }
                });
                helper.setBackgroundRes(R.id.tv_order_gonow,R.drawable.shape_staking_item_nowgo_btnbgd);
            } else {
                //不可以挖矿
                helper.setText(R.id.tv_order_gonow, getString(R.string.easteregg_hint_noopen));
                helper.setOnClickListener(R.id.tv_order_gonow, null);
                helper.setGone(R.id.tv_waittime, false);
                helper.setGone(R.id.tv_waitnum, false);
                helper.setBackgroundRes(R.id.tv_order_gonow,R.drawable.shape_staking_item_nowgo_btnbgd_noopen);
            }
        }
    }
}
